<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>PyMOTW: os(4) &mdash; PyMOTW Document v1.6 documentation</title>
    <link rel="stylesheet" href="../_static/mytest.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.6',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <!-- comment utility -->
    <link rel="stylesheet" href="../_static/comment.css" type="text/css" />
    <script type="text/javascript" src="../_static/md5.js"></script>
    <script type="text/javascript" src="../_static/comment.js"></script>
    <!-- end -->
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="PyMOTW Document v1.6 documentation" href="../index.html" />
    <link rel="next" title="PyMOTW: pickle & cPickle" href="pickle.html" />
    <link rel="prev" title="PyMOTW: os(3)" href="os3.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="pickle.html" title="PyMOTW: pickle & cPickle"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="os3.html" title="PyMOTW: os(3)"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">PyMOTW Document v1.6 documentation</a> &raquo;</li> 
      </ul>
    </div> 
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">PyMOTW: os(4)</a><ul>
<li><a class="reference external" href="#id1">描述</a></li>
<li><a class="reference external" href="#id2">申明</a></li>
<li><a class="reference external" href="#id3">运行外部命令</a></li>
<li><a class="reference external" href="#os-fork">使用os.fork()创建进程</a></li>
<li><a class="reference external" href="#id4">等待一个子进程</a></li>
<li><a class="reference external" href="#spawn">Spawn(孵化)</a></li>
<li><a class="reference external" href="#id5">结论</a></li>
<li><a class="reference external" href="#id6">参考</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="os3.html"
                                  title="previous chapter">PyMOTW: os(3)</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="pickle.html"
                                  title="next chapter">PyMOTW: pickle &amp; cPickle</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/documents/os4.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div> 

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="pymotw-os-4">
<h1>PyMOTW: os(4)<a class="headerlink" href="#pymotw-os-4" title="Permalink to this headline">¶</a></h1>
<div class="section" id="id1">
<h2>描述<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>这周, 我总结整个os模块(但保留os.path的内容作为将来独立的一篇)并讨论一些有利于处理多进程的函数. 我在part2中已经介绍了管道的使用, 这周我们来看下system(), fork(), exec()这3个函数和他们之间的关系.</p>
</div>
<div class="section" id="id2">
<h2>申明<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<p>这里的许多函数都有可移植性限制. 可以查看subprocess模块以获得一种更一致的平台独立的处理进程方式.</p>
</div>
<div class="section" id="id3">
<h2>运行外部命令<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h2>
<p>最简单的运行一条单独命令, 没有一点交互的方式是使用os.system(). 他获取一个字符串, 这个字符串就是一将被命令行执行的命令, 通过一个shell中的子进程来执行.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>

<span class="c"># Simple command</span>
<span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s">&#39;ls -l&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python os_system_example.py
total 168
-rw-r--r-- 1 dhellman dhellman 0 May 27 06:58 __init__.py
-rw-r--r-- 1 dhellman dhellman 1391 Jun 10 09:36 os_access.py
-rw-r--r-- 1 dhellman dhellman 1383 May 27 09:23 os_cwd_example.py
-rw-r--r-- 1 dhellman dhellman 1535 Jun 10 09:36 os_directories.py
-rw-r--r-- 1 dhellman dhellman 1613 May 27 09:23 os_environ_example.py
-rw-r--r-- 1 dhellman dhellman 2816 Jun 3 08:34 os_popen_examples.py
-rw-r--r-- 1 dhellman dhellman 1438 May 27 09:23 os_process_id_example.py
-rw-r--r-- 1 dhellman dhellman 1887 May 27 09:23 os_process_user_example.py
-rw-r--r-- 1 dhellman dhellman 1545 Jun 10 09:36 os_stat.py
-rw-r--r-- 1 dhellman dhellman 1638 Jun 10 09:36 os_stat_chmod.py
-rw-r--r-- 1 dhellman dhellman 1452 Jun 10 09:36 os_symlinks.py
-rw-r--r-- 1 dhellman dhellman 1279 Jun 17 12:17 os_system_example.py
-rw-r--r-- 1 dhellman dhellman 1672 Jun 10 09:36 os_walk.py</pre>
</div>
<p>由于命令是直接被传递到处理shell中, 所以它可以包含shell语法, 比如通配符或环境变量:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Command with shell expansion</span>
<span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s">&#39;ls -l $HOME&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>total 40
-rwx------ 1 dhellman dhellman 1328 Dec 13 2005 %backup%~
drwx------ 11 dhellman dhellman 374 Jun 17 12:11 Desktop
drwxr-xr-x 15 dhellman dhellman 510 May 27 07:50 Devel
drwx------ 29 dhellman dhellman 986 May 31 17:01 Documents
drwxr-xr-x 45 dhellman dhellman 1530 Jun 17 12:12 DownloadedApps
drwx------ 55 dhellman dhellman 1870 May 22 14:53 Library
drwx------ 8 dhellman dhellman 272 Mar 4 2006 Movies
drwx------ 10 dhellman dhellman 340 Feb 14 10:54 Music
drwx------ 12 dhellman dhellman 408 Jun 17 01:00 Pictures
drwxr-xr-x 5 dhellman dhellman 170 Oct 1 2006 Public
drwxr-xr-x 15 dhellman dhellman 510 May 12 15:19 Sites
drwxr-xr-x 4 dhellman dhellman 136 Jan 23 2006 iPod
-rw-r--r-- 1 dhellman dhellman 105 Mar 7 11:48 pgadmin.log
drwxr-xr-x 3 dhellman dhellman 102 Apr 29 16:32 tmp</pre>
</div>
<p>除非你是直接在后台中运行这命令, 不然的话, 直到命令执行完毕, 调用 <tt class="docutils literal"><span class="pre">os.system()</span></tt> 的程序都会处于阻断状态. 子进程中的标准输入, 输出, 和错误输出默认被绑定到调用者的合适的流中. 但是也可以通过shell语法重定向到其他地方.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="k">print</span> <span class="s">&#39;Calling...&#39;</span>
<span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s">&#39;date; (sleep 3; date) &amp;&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="s">&#39;Sleeping...&#39;</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">5</span><span class="p">)</span>
</pre></div>
</div>
<p>这就是shell的魔力, 尽管还有更好的实现方式.</p>
<div class="highlight-python"><pre>$ python os_system_background.py
Calling...
Sun Jun 17 12:27:20 EDT 2007
Sleeping...
Sun Jun 17 12:27:23 EDT 2007</pre>
</div>
</div>
<div class="section" id="os-fork">
<h2>使用os.fork()创建进程<a class="headerlink" href="#os-fork" title="Permalink to this headline">¶</a></h2>
<p>符合POSIX标准的函数 <tt class="docutils literal"><span class="pre">fork()</span></tt> 和 <tt class="docutils literal"><span class="pre">exec*()</span></tt> (在Mac OS X, Linux和其他类UNIX系统上可用)通过os模块都是可用的. 很多书已经很全面可靠的描述了这些函数的使用, 所以检查你的库手册, 或者去书店寻找进一步细节.</p>
<p>创建一个新进程作为当前进程的一个复本, 可以使用 <tt class="docutils literal"><span class="pre">os.fork()</span></tt> :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>

<span class="k">if</span> <span class="n">pid</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;Child process id:&#39;</span><span class="p">,</span> <span class="n">pid</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;I am the child&#39;</span>
</pre></div>
</div>
<p>每次运行这个事例代码时, 你的输出变化给予你系统的当前状态, 但是它应该看起来像如下:</p>
<div class="highlight-python"><pre>$ python os_fork_example.py
Child process id: 5883
I am the child</pre>
</div>
<p>当fork之后, 你结束这两个运行着相同代码的进程. 可以检查返回值来直到你在哪个进程中. 如果它是0, 表示你在子进程中, 如果不是0, 则表示你在父进程中, 它返回的值是其子进程的进程id.</p>
<p>对于父进程来说, 发送给子进程信号是有必要的. 这个的设置有点复杂, 使用signal模块, 让我们通过具体代码来描述其使用吧. 首先我们定义一个信号处理句柄, 以便在收到相应信号时调用.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">signal</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="k">def</span> <span class="nf">signal_usr1</span><span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">frame</span><span class="p">):</span>
    <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&#39;Received USR1 in process </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">pid</span>
</pre></div>
</div>
<p>然后我们创建子进程, 并在父进程中, 通过 <tt class="docutils literal"><span class="pre">os.kill()</span></tt> 发送一个USR1信号之前暂停一段时间. 这短的暂停让子进程有时间去设置信号处理句柄.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="s">&#39;Forking...&#39;</span>
<span class="n">child_pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
<span class="k">if</span> <span class="n">child_pid</span><span class="p">:</span> <span class="c">## 这个是父进程执行的代码</span>
    <span class="k">print</span> <span class="s">&#39;PARENT: Pausing before sending signal...&#39;</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;PARENT: Signaling </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">child_pid</span>
    <span class="n">os</span><span class="o">.</span><span class="n">kill</span><span class="p">(</span><span class="n">child_pid</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIGUSR1</span><span class="p">)</span>
</pre></div>
</div>
<p>在子进程中, 我们设置信号处理句柄后睡眠一段时间来让父进程有时间去发送信号给我们:</p>
<div class="highlight-python"><pre>else:
    print 'CHILD: Setting up signal handler'
    signal.signal(signal.SIGUSR1, signal_usr1)
    print 'CHILD: Pausing to wait for signal'
    time.sleep(5)</pre>
</div>
<p>当然, 在实际的程序中, 你也可能不需要(不想)调用sleep.</p>
<div class="highlight-python"><pre>$ python os_kill_example.py
Forking...
PARENT: Pausing before sending signal...
CHILD: Setting up signal handler
CHILD: Pausing to wait for signal
PARENT: Signaling 6053
Received USR1 in process 6053</pre>
</div>
<p>正如你所看到的, 一个简单的处理子进程各自行为的方式是简单 <tt class="docutils literal"><span class="pre">fork()</span></tt> 函数的返回值并使用if分支实现. 对于更复杂的行为, 就需要更多的分离(独立)的代码,而不是简单的分支. 在其他的例子中,你可以使用一个已经封装好的程序. 对于这两种情况, 你可以使用 <tt class="docutils literal"><span class="pre">os.exec*()</span></tt> 系列函数来运行其他程序. 当你&#8221;exec&#8221;一个程序, 程序中的代码会代替你进程中已存在的那些代码.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">child_pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
<span class="k">if</span> <span class="n">child_pid</span><span class="p">:</span>
    <span class="n">os</span><span class="o">.</span><span class="n">waitpid</span><span class="p">(</span><span class="n">child_pid</span><span class="p">,</span> <span class="mf">0</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">os</span><span class="o">.</span><span class="n">execlp</span><span class="p">(</span><span class="s">&#39;ls&#39;</span><span class="p">,</span> <span class="s">&#39;ls&#39;</span><span class="p">,</span> <span class="s">&#39;-l&#39;</span><span class="p">,</span> <span class="s">&#39;/tmp/&#39;</span><span class="p">)</span> <span class="c">## 执行多个子进程</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python os_exec_example.py

total 40
drwxr-xr-x 2 dhellman wheel 68 Jun 17 14:35 527
prw------- 1 root wheel 0 Jun 15 19:24 afpserver_PIPE
drwx------ 3 dhellman wheel 102 Jun 17 12:13 emacs527
drwxr-xr-x 2 dhellman wheel 68 Jun 16 05:01 hsperfdata_dhellmann
-rw------- 1 nobody wheel 12 Jun 17 13:55 objc_sharing_ppc_4294967294
-rw------- 1 dhellman wheel 144 Jun 17 14:32 objc_sharing_ppc_527
-rw------- 1 security wheel 24 Jun 17 07:09 objc_sharing_ppc_92
drwxr-xr-x 4 dhellman dhellman 136 Jun 8 03:16 var_backups</pre>
</div>
<p>有很多 <tt class="docutils literal"><span class="pre">exec*()</span></tt> 的变种, 它们依赖于你可能使用的参数, 如, 你是否想要路径和父进程的环境变量都被复制到子进程中, 等等. 细节可参见库文档.</p>
<p>对于所有变种, 它们的第一个参数是一个路径或者文件名, 剩下的参数控制如何运行相应程序. 它们要么作为命令行参数被传递, 要么覆盖进程&#8221;环境&#8221;(可查看 <tt class="docutils literal"><span class="pre">os.environ</span></tt> 和 <tt class="docutils literal"><span class="pre">os.getenv</span></tt> ).</p>
</div>
<div class="section" id="id4">
<h2>等待一个子进程<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h2>
<p>假设说你使用了多个进程来突破Python的线程限制和 <tt class="docutils literal"><span class="pre">GIL</span></tt>. 如果你开始了多个进程来运行各自的任务, 你希望在开始新的进程之前等待其中一个或多个的结束, 以此来避免服务器的超载. 这里有一些使用 <tt class="docutils literal"><span class="pre">wait()</span></tt> 和相关函数来实现它的不同方法.</p>
<p>如果你不关心, 或者你已经知道, 哪个可能会首先退出 <tt class="docutils literal"><span class="pre">os.wait()</span></tt> 的子进程, 并且这个子进程会尽快的返回任何存在:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mf">3</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;PARENT: Forking </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">i</span>
    <span class="n">worker_pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">worker_pid</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&#39;WORKER </span><span class="si">%s</span><span class="s">: Starting&#39;</span> <span class="o">%</span> <span class="n">i</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">2</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">&#39;WORKER </span><span class="si">%s</span><span class="s">: Finishing&#39;</span> <span class="o">%</span> <span class="n">i</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mf">3</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;PARENT: Waiting for </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">i</span>
    <span class="n">done</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&#39;PARENT:&#39;</span><span class="p">,</span> <span class="n">done</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><tt class="docutils literal"><span class="pre">os.wait()</span></tt> 的返回值是包含进程号和退出状态(一个16位的数字, 它的低字节是一个杀死该进程的信号数字, 它的高字节是退出状态)的一个元组.</p>
</div>
<div class="highlight-python"><pre>$ python os_wait_example.py

PARENT: Forking 0
PARENT: Forking 1
PARENT: Forking 2
PARENT: Waiting for 0
WORKER 0: Starting
WORKER 1: Starting
WORKER 2: Starting
WORKER 0: Finishing
PARENT: (6501, 0)
PARENT: Waiting for 1
WORKER 1: Finishing
PARENT: (6502, 256)
PARENT: Waiting for 2
WORKER 2: Finishing
PARENT: (6503, 512)</pre>
</div>
<p>如果你想等待一个特定的进程, 可以使用 <tt class="docutils literal"><span class="pre">os.waitpid()</span></tt> .</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="n">workers</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mf">3</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;PARENT: Forking </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">i</span>
    <span class="n">worker_pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">worker_pid</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&#39;WORKER </span><span class="si">%s</span><span class="s">: Starting&#39;</span> <span class="o">%</span> <span class="n">i</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">2</span> <span class="o">+</span> <span class="n">i</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">&#39;WORKER </span><span class="si">%s</span><span class="s">: Finishing&#39;</span> <span class="o">%</span> <span class="n">i</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">workers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">worker_pid</span><span class="p">)</span>

<span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">workers</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;PARENT: Waiting for </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">pid</span>
    <span class="n">done</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">waitpid</span><span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="mf">0</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;PARENT:&#39;</span><span class="p">,</span> <span class="n">done</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python os_waitpid_example.py
PARENT: Forking 0
WORKER 0: Starting
PARENT: Forking 1
WORKER 1: Starting
PARENT: Forking 2
WORKER 2: Starting
PARENT: Waiting for 6547
WORKER 0: Finishing
PARENT: (6547, 0)
PARENT: Waiting for 6548
WORKER 1: Finishing
PARENT: (6548, 256)
PARENT: Waiting for 6549
WORKER 2: Finishing
PARENT: (6549, 512)</pre>
</div>
<p><tt class="docutils literal"><span class="pre">wait3()</span></tt> 和 <tt class="docutils literal"><span class="pre">wait4()</span></tt> 函数也是类似的方式, 但它们返回更多关于子进程的细节信息, 如进程号, 退出状态, 资源使用情况等.</p>
</div>
<div class="section" id="spawn">
<h2>Spawn(孵化)<a class="headerlink" href="#spawn" title="Permalink to this headline">¶</a></h2>
<p>方便起见, <tt class="docutils literal"><span class="pre">os.spawn*()</span></tt> 系列函数将 <tt class="docutils literal"><span class="pre">fork()</span></tt> 和 <tt class="docutils literal"><span class="pre">exec*()</span></tt> 调用写在一条语句中:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">os</span><span class="o">.</span><span class="n">spawnlp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_WAIT</span><span class="p">,</span> <span class="s">&#39;ls&#39;</span><span class="p">,</span> <span class="s">&#39;ls&#39;</span><span class="p">,</span> <span class="s">&#39;-l&#39;</span><span class="p">,</span> <span class="s">&#39;/tmp/&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python os_exec_example.py
total 40
drwxr-xr-x 2 dhellman wheel 68 Jun 17 14:35 527
prw------- 1 root wheel 0 Jun 15 19:24 afpserver_PIPE
drwx------ 3 dhellman wheel 102 Jun 17 12:13 emacs527
drwxr-xr-x 2 dhellman wheel 68 Jun 16 05:01 hsperfdata_dhellmann
-rw------- 1 nobody wheel 12 Jun 17 13:55 objc_sharing_ppc_4294967294
-rw------- 1 dhellman wheel 144 Jun 17 14:32 objc_sharing_ppc_527
-rw------- 1 security wheel 24 Jun 17 07:09 objc_sharing_ppc_92
drwxr-xr-x 4 dhellman dhellman 136 Jun 8 03:16 var_backups</pre>
</div>
</div>
<div class="section" id="id5">
<h2>结论<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h2>
<p>还有其他很多在处理多进程时需要考虑的东西, 比如, 信号处理, 多进程文件读写等. 所有这些话题都在参考书目(如 <a class="reference external" href="http://www.amazon.com/Programming-Environment-Addison-Wesley-Professional-Computing/dp/0201433079/ref=pd_bbs_3/002-2842372-4768037?ie=UTF8&amp;s=books&amp;amp;qid=1182098757&amp;sr=8-3">Advanced Programming in the UNIX(R) Environment</a> )中有讲述.</p>
</div>
<div class="section" id="id6">
<h2>参考<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://www.ibm.com/developerworks/aix/library/au-unixprocess.html">Delve into UNIX process creation</a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="pickle.html" title="PyMOTW: pickle & cPickle"
             >next</a> |</li>
        <li class="right" >
          <a href="os3.html" title="PyMOTW: os(3)"
             >previous</a> |</li>
        <li><a href="../index.html">PyMOTW Document v1.6 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 2009, vbarter &amp; liz.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>